package com.seeyon.apps.ext.cop.button.demo;

import snail.common.utils.JSONHelper;
import snail.common.utils.MapHelper;
import snail.common.utils.db.DBHelper;
import snail.common.utils.db.DataSourceHelper;
import snail.common.web.HTTPHelper;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;

public class DeptMap {
    //正式数据源
    private static String urls = "jdbc:mysql://10.211.55.2:3306/cop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";// 连接URL字符串
    private static String user = "root";// 数据库用户名
    private static String password = "Mysql123456";// 用户密码

    /**
     * 查找id查列表信息
     * @param id
     * @return
     */
    public static List<Map<String, Object>> selectDepRoleCop(String id) {

        // 登录认证逻辑，用户名和密码正确时返回true即可
        DataSource ds = DataSourceHelper.getMySQL6XDatasource(urls, user, password);
        DBHelper dbhelper = DBHelper.getInstance(ds);
        try {
            List<Map<String, Object>> userList = dbhelper.queryList("select * from formmain_0040 where id = ?", id);
            return userList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String[] a = {"-8730856250813888936","-8621092802048340409","-2924345682710999493","5223180741731370604"}; // id
        HashMap<String, List> v = new HashMap<>();

        for (int i = 0; i<a.length ;i++){

            String deptId = (String) selectDepRoleCop(a[i]).get(0).get("field0037");
            List<String> values = new ArrayList<>();
            // 部门
            if (v.containsKey(deptId)) {
                List<String> value = v.get(deptId);
                String mmBody = MapHelper.builder("序号1",i+1)
                        .put("人员", selectDepRoleCop(a[i]).get(0).get("field0036"))
                        .put("部门", selectDepRoleCop(a[i]).get(0).get("field0037"))
                        .put("宿舍", selectDepRoleCop(a[i]).get(0).get("field0038"))
                        .put("作训服", selectDepRoleCop(a[i]).get(0).get("field0040"))
                        .put("培训", selectDepRoleCop(a[i]).get(0).get("field0039"))
                        .put("薪资", selectDepRoleCop(a[i]).get(0).get("field0048"))
                        .put("社保", selectDepRoleCop(a[i]).get(0).get("field0045"))
                        .put("合同", selectDepRoleCop(a[i]).get(0).get("field0047"))
                        .put("指纹", selectDepRoleCop(a[i]).get(0).get("field0041"))
                        .toJson();
                value.add(mmBody);
                v.put(deptId,value);
            }
            else {

                String mmBody = MapHelper.builder("序号1",i+1)
                        .put("人员", selectDepRoleCop(a[i]).get(0).get("field0036"))
                        .put("部门", selectDepRoleCop(a[i]).get(0).get("field0037"))
                        .put("宿舍", selectDepRoleCop(a[i]).get(0).get("field0038"))
                        .put("作训服", selectDepRoleCop(a[i]).get(0).get("field0040"))
                        .put("培训", selectDepRoleCop(a[i]).get(0).get("field0039"))
                        .put("薪资", selectDepRoleCop(a[i]).get(0).get("field0048"))
                        .put("社保", selectDepRoleCop(a[i]).get(0).get("field0045"))
                        .put("合同", selectDepRoleCop(a[i]).get(0).get("field0047"))
                        .put("指纹", selectDepRoleCop(a[i]).get(0).get("field0041"))
                        .toJson();
                values.add(mmBody);
                v.put(deptId, values);
            }

        }
        System.out.println("dkkk" + v);
        for (Object j : v.entrySet()){
            Map.Entry entry = (Map.Entry) j;
            System.out.println(entry.getKey() + "|" + entry.getValue());


            HashMap mmmBoy = new HashMap();
            mmmBoy.put("申请人","4608347538121967090");
            mmmBoy.put("申请部门", entry.getKey());

            List nulls = new ArrayList();
            HashMap nullss = new HashMap();
            HashMap mmBody = new HashMap();
            mmBody.put("formmain_0056", mmmBoy);
            mmBody.put("formson_0057", JSONHelper.parse(String.valueOf(entry.getValue())));
            mmBody.put("thirdAttachments", nulls);
            mmBody.put("changedFields", nullss);


            HashMap mBody = new HashMap();
            mBody.put("data",mmBody);
            mBody.put("templateCode", "RZ00001");
            mBody.put("draft", "0");
            mBody.put("subject", "");
            mBody.put("useNewDataStructure", false);
            mBody.put("doTrigger", false);

            String body = MapHelper.builder("appName", "collaboration")
                    .put("data", mBody)
                    .toJson();
            String url = "http://10.37.129.2:8090/seeyon/rest/bpm/process/start";
            Map<String, Object> header = MapHelper.builder("Content-Type", "application/json")
                    .put("token", "457a847e-4bac-478e-850b-726f71ba5514")
                    .body();

            Map<String, Object> result = (Map<String, Object>) JSONHelper.parse(HTTPHelper.post(url, body, header));

            System.out.println(result);
        }
    }
}
